package com.example.windowdemo.window;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.example.windowdemo.callback.WindowObserver;
import com.example.windowdemo.callback.WindowStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: njb
 * @date: 2023/7/31 11:37
 * @desc:
 */
public class Window<T> {
    public Context context;
    private boolean isFullScreen;
    private boolean hasTitleBar;
    public int width;
    public int height;
    public T data;
    private static final String TAG = "WindowLog";
    public WindowManager.LayoutParams layoutParams;
    public View contentView;
    private List<WindowObserver<T>> observers = new ArrayList<>();

    public Window(Context context) {
        this.context = context;
        this.layoutParams = new WindowManager.LayoutParams();
        initView();
    }

    private void initView() {

    }

    public void setFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
        if (isFullScreen) {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags &= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setTitleBar(boolean hasTitleBar) {
        this.hasTitleBar = hasTitleBar;
        if (hasTitleBar) {
            layoutParams.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
        layoutParams.width = width;
        layoutParams.height = height;
    }

    public void setPosition(int x, int y) {
        layoutParams.x = x;
        layoutParams.y = y;
    }

    public void setGravity(int gravity) {
        layoutParams.gravity = gravity;
    }

    public void setData(T data){
        boolean dataChanged = !Objects.equals(this.data, data);
        this.data = data;
        if (dataChanged) {
            for (WindowObserver<T> observer : observers) {
                observer.onWindowDataChange(this, data);
            }
        }
        Log.d(TAG,"===获取到的数据为==="+ data.toString());
    }

    public T getData() {
        return data;
    }

    public void addView(View view) {
        this.contentView = view;
    }


    protected boolean autoRegisterEventBus() {
        return false;
    }


    /**
     * 事件分发
     */
    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        return false;
    }

    public void registerObserver(WindowObserver<T> observer) {
        observers.add(observer);
    }

    public void unregisterObserver(WindowObserver<T> observer) {
        observers.remove(observer);
    }

    public void show() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(contentView, layoutParams);
        for (WindowObserver<T> observer : observers) {
            observer.onWindowShow(this);
        }
    }

    public void hide() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.removeView(contentView);
        for (WindowObserver<T> observer : observers) {
            observer.onWindowHide(this);
        }
    }

    /**
     * 视图分离
     */
    public void onDetachedFromWindow() {
    }

}
